package com.niiwoo.civet.trade.service.local.notify.impl;

import com.niiwoo.civet.trade.dao.entity.Project;
import com.niiwoo.civet.trade.dao.entity.RefundNotifyRecord;
import com.niiwoo.civet.trade.service.local.newRefund.notify.dto.RefundNotifyMessageDTO;
import com.niiwoo.civet.trade.service.local.notify.AbstractNotifier;
import com.niiwoo.civet.user.dto.request.AppButtonDTO;
import com.niiwoo.civet.user.dto.request.JPushExtDTO;
import com.niiwoo.civet.user.dto.request.UserLetterRequestDTO;
import com.niiwoo.civet.user.enums.JPushEnum;
import com.niiwoo.civet.user.enums.UserLetterTypeEnum;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;

import java.math.BigDecimal;
import java.util.*;

public abstract class AbstractRepaymentSuccessNotifier<T extends RefundNotifyMessageDTO> extends AbstractNotifier<T> {
    @Value("${h5-borrow-detail-url}")
    private String h5BorrowDetailUrl;

    @Override
    protected List<? extends UserNotifyInfo> getUserNotifyInfoList(T t) {
		RepaymentSuccessNotifyInfo info = new RepaymentSuccessNotifyInfo();
		
		Project project = t.getProject();
		RefundNotifyRecord refundNotifyRecord = t.getRefundNotifyRecord();
		
		info.setBorrowerUserId(project.getBorrowUserId());
		info.setProjectId(project.getProjectId());
		info.setProjectTitle(t.getProjectTitle());
		info.setAmount(t.getAmount());
		info.setCurrentPeriod(refundNotifyRecord.getCurrentPeriod());
		info.setTotalPeriod(project.getDeadline());
		return Collections.singletonList(info);
    }

    @Override
    protected SMSRequest getSMSRequestDTO(UserNotifyInfo info) {
        return null;
    }

    @Override
    protected UserLetterRequestDTO getUserLetterRequestDTO(UserNotifyInfo info) {
        RepaymentSuccessNotifyInfo p = (RepaymentSuccessNotifyInfo) info;

        String borrowerUserId = p.getBorrowerUserId();
        Long projectId = p.getProjectId();
        String projectTitle = p.projectTitle;
        BigDecimal amount = p.getAmount();
        String periodString = p.getCurrentPeriod()+"/" + p.getTotalPeriod();

        UserLetterRequestDTO letterRequest = new UserLetterRequestDTO();

        //发站内信
        AppButtonDTO messageButton = new AppButtonDTO();
        messageButton.setLabel("项目群");
        messageButton.setButtonType(AppButtonDTO.ButtonTypeEnum.PROJECT_GROUP);
        Map<String, Object> messageButtonParam = new HashMap<>();
        messageButtonParam.put("ProjectId", projectId);
        messageButton.setParams(messageButtonParam);

        AppButtonDTO loanButton = new AppButtonDTO();
        loanButton.setLabel("借款详情");
        loanButton.setButtonType(AppButtonDTO.ButtonTypeEnum.MONMY_LOAN_DETAILEY_2);
        Map<String, Object> loanButtonParam = new HashMap<>();
        loanButtonParam.put("ProjectId", projectId);
        loanButtonParam.put("LoadServerUrl", h5BorrowDetailUrl+projectId);
        loanButton.setParams(loanButtonParam);

        List<AppButtonDTO> listForButton = new ArrayList<>();
        listForButton.add(messageButton);
        listForButton.add(loanButton);

        letterRequest.setLetterButtonList(listForButton);

        List<Object> letterTemplateParamList =  Arrays.asList(projectId, projectTitle, periodString, amount);
        List<Object> jPushTemplateParamList =  Arrays.asList(projectTitle, periodString, amount);
        letterRequest.setLetterTemplateParamList(letterTemplateParamList);
        letterRequest.setJpushTemplateParamList(jPushTemplateParamList);
        letterRequest.setToUserId(borrowerUserId);
        letterRequest.setProjectId(projectId);
        letterRequest.setUserLetterTypeEnum(UserLetterTypeEnum.LETTER_AND_JPUSH);
        JPushExtDTO jPushExtDTO = new JPushExtDTO();
        jPushExtDTO.setRedirectPageEnum(JPushEnum.RedirectPageEnum.NOTICE_LIST_PAGE);
        letterRequest.setJPushExt(jPushExtDTO);

        return letterRequest;
    }

    @Getter
    @Setter
    protected static class RepaymentSuccessNotifyInfo extends UserNotifyInfo {

        private String borrowerUserId;

        private Long projectId;

        private String projectTitle;

        private BigDecimal amount;

        private Integer currentPeriod;

        private Integer totalPeriod;
    }

}
